#!/bin/bash -e

PROG=$(basename $0)
PROG_DIR=$(dirname $0)

function usage {
    cat <<EOF 1>&2
usage: $PROG [options...] SOURCEDIR
Options:
  -i IMAGE  Name of the docker image (including tag) to use as package build environment.
  -o DIR    Destination directory to store packages to.
  -d DIR    Directory that contains other deb packages that need to be installed before build.
EOF
    exit 1
}

function fatal {
    echo "$PROG: ${1:-"Unknown Error"}" 1>&2
    exit 1
}

function abspath {
    echo $(cd "$1" && pwd)
}


###########################################################################

[[ $# -eq 0 ]] && usage

while getopts "i:o:d:h" opt; do
    case $opt in
        i)
            image="$OPTARG"
            ;;
        o)
            outdir="$OPTARG"
            ;;
        d)
            depdir="$OPTARG"
            ;;
        h)
            usage
            ;;
        *)
            usage
            ;;
    esac
done

shift $(($OPTIND - 1))
srcdir=$1
docker_args=""

# Check that mandatory parameters are valid
[[ !    "$outdir"        ]] && fatal "output directory was not given (-o DIR)"
[[ ! -d "$outdir"        ]] && fatal "output directory does not exist: $outdir"
[[ !    "$srcdir"        ]] && fatal "source directory not given"
[[ ! -r "$srcdir/debian" ]] && fatal "source direcotry does not contain debian sub directory"
[[ !    "$image"         ]] && fatal "docker image name not given (-i IMAGE)"

# Check that optional parameters are valid
if [[ "$depdir" ]]; then
    [[ ! -d "$depdir" ]] && fatal "dependency directory given but does not exist: $depdir"
    docker_args+="-v $(abspath "$depdir"):/dependencies:ro "
fi

docker_args+="-v $(abspath "$srcdir"):/source-ro:ro -v $(abspath "$outdir"):/output -v $(cd $PROG_DIR; pwd)/build-helper.sh:/build-helper.sh:ro "

# Pass current UID and GID to container, so that it can change the
# ownership of output files which are otherwise writen to outdir as
# root
docker_args+="-e USER=$(id -u) -e GROUP=$(id -g) "

# Comment following out if you want to keep container after execution
# for debugging
docker_args+="--rm "

# Workaround for the Qt RCC problem, see:
# https://stackoverflow.com/a/52084936
docker_args+="--security-opt seccomp:unconfined "

cmd="docker run $docker_args $image /build-helper.sh"

echo "Running docker:"
echo "$cmd"

exec $cmd
